import type { MaFormItem } from "@mineadmin/form";
import hasAuth from "@/utils/permission/hasAuth.ts";
import MaDictSelect from "@/components/ma-dict-picker/ma-dict-select.vue";
import MaDictRadio from "@/components/ma-dict-picker/ma-dict-radio.vue";
import { concat } from "lodash-es";
import { rdOrganizeAreaStandStatusDictData } from "./common.tsx";
import { page as getAreaList } from "@/plugins/ai-warning/organization/api/rdOrganizeArea.ts";
import MaRemoteSelect from "@/components/ma-remote-select/index.vue";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: any
): MaFormItem[] {
  // 为新增表单设置默认值
  if (formType === "add") {
    model.status = 1; // 默认选中正常状态
  }

  return concat([
    {
      label: "所属工区",
      prop: "area_id",
      render: () => MaRemoteSelect,
      renderProps: {
        api: getAreaList,
        apiParams: {
          page: 1,
          pageSize: 1000,
        },
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((area: any) => ({
              label: area.name,
              value: area.id,
            }));
          }
          return [];
        },
        placeholder: "请选择所属工区",
        filterable: true,
        clearable: true,
      },
      itemProps: {
        rules: [{ required: true, message: "请选择所属工区" }],
      },
    },
    {
      label: "站点名称",
      prop: "name",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "站点名称" }) },
      itemProps: {
        rules: [{ required: true, message: "请输入站点名称" }],
      },
    },
    {
      label: "简称",
      prop: "abbr",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "简称" }) },
      itemProps: {
        rules: [{ required: false }],
      },
    },
    {
      label: "地址",
      prop: "addr",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "地址" }) },
      itemProps: {
        rules: [{ required: false }],
      },
    },
    {
      label: "经度",
      prop: "longitude",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "经度" }),
        type: "number",
        step: 0.000001,
        precision: 6,
      },
      // itemProps: {
      //   rules: [
      //     { required: true, message: "请输入经度" },
      //     {
      //       pattern: /^-?((1[0-7]\d)|(\d{1,2}))(\.\d{1,6})?$/,
      //       message: "请输入有效的经度值(-180到180)",
      //     },
      //   ],
      // },
    },
    {
      label: "纬度",
      prop: "latitude",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: "纬度" }),
        type: "number",
        step: 0.000001,
        precision: 6,
      },
      // itemProps: {
      //   rules: [
      //     { required: true, message: "请输入纬度" },
      //     {
      //       pattern: /^-?([0-8]?\d)(\.\d{1,6})?$/,
      //       message: "请输入有效的纬度值(-90到90)",
      //     },
      //   ],
      // },
    },
    {
      label: "状态",
      prop: "status",
      render: () => MaDictRadio,
      renderProps: {
        multiple: false,
        data: rdOrganizeAreaStandStatusDictData(),
      },
      itemProps: {
        rules: [{ required: true, message: "请选择状态" }],
      },
      defaultValue: 1,
    },
    {
      label: "备注",
      prop: "remark",
      render: "input",
      renderProps: {
        type: "textarea",
        placeholder: t("form.pleaseInput", { msg: "备注" }),
        rows: 3,
      },
      itemProps: {
        rules: [{ required: false }],
      },
    },
  ]);
}
